{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 使用 {func}`tvm._ffi._init_api` 管理 TVM 插件\n",
    "\n",
    "[](./ext-cpp) 调用细节可以借助 FFI 函数 {func}`tvm._ffi._init_api` 进行隐藏。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "ename": "ModuleNotFoundError",
     "evalue": "No module named 'tvm'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mModuleNotFoundError\u001b[0m                       Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[1], line 1\u001b[0m\n\u001b[0;32m----> 1\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtvm_book\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mtvm_ext\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlibinfo\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m load_lib\n\u001b[1;32m      3\u001b[0m _LIB_EXT, _LIB_EXT_NAME \u001b[38;5;241m=\u001b[39m load_lib(name\u001b[38;5;241m=\u001b[39m\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mlibtvm_ext.so\u001b[39m\u001b[38;5;124m\"\u001b[39m, search_path\u001b[38;5;241m=\u001b[39m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124m../cpp/sym_add/outputs/libs\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[1;32m      5\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mtvm\u001b[39;00m\n",
      "File \u001b[0;32m/media/pc/data/lxw/ai/tvm-book/src/tvm_book/tvm_ext/libinfo.py:4\u001b[0m\n\u001b[1;32m      2\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01msys\u001b[39;00m\u001b[38;5;241m,\u001b[39m \u001b[38;5;21;01mos\u001b[39;00m\n\u001b[1;32m      3\u001b[0m \u001b[38;5;28;01mimport\u001b[39;00m \u001b[38;5;21;01mctypes\u001b[39;00m\n\u001b[0;32m----> 4\u001b[0m \u001b[38;5;28;01mfrom\u001b[39;00m \u001b[38;5;21;01mtvm\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01m_ffi\u001b[39;00m\u001b[38;5;21;01m.\u001b[39;00m\u001b[38;5;21;01mlibinfo\u001b[39;00m \u001b[38;5;28;01mimport\u001b[39;00m find_lib_path\n\u001b[1;32m      6\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mload_lib\u001b[39m(name: \u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m|\u001b[39m\u001b[38;5;28mlist\u001b[39m[\u001b[38;5;28mstr\u001b[39m]\u001b[38;5;241m|\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m, search_path: \u001b[38;5;28mstr\u001b[39m\u001b[38;5;241m|\u001b[39m\u001b[38;5;28mlist\u001b[39m[\u001b[38;5;28mstr\u001b[39m]\u001b[38;5;241m|\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m      7\u001b[0m \u001b[38;5;250m    \u001b[39m\u001b[38;5;124;03m\"\"\"通过搜索可能的路径加载库\u001b[39;00m\n\u001b[1;32m      8\u001b[0m \u001b[38;5;124;03m    \u001b[39;00m\n\u001b[1;32m      9\u001b[0m \u001b[38;5;124;03m    Arg:\u001b[39;00m\n\u001b[1;32m     10\u001b[0m \u001b[38;5;124;03m        name: 需要导入的库名称（比如 ``libtvm_ext.so``），可为空（则为 ``libtvm.so``），也可为名称列表（暂未实现）\u001b[39;00m\n\u001b[1;32m     11\u001b[0m \u001b[38;5;124;03m        search_path: 搜索路径，可为空，也可为名称列表\u001b[39;00m\n\u001b[1;32m     12\u001b[0m \u001b[38;5;124;03m    \"\"\"\u001b[39;00m\n",
      "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'tvm'"
     ]
    }
   ],
   "source": [
    "from tvm_book.tvm_ext.libinfo import load_lib\n",
    "\n",
    "_LIB_EXT, _LIB_EXT_NAME = load_lib(name=\"libtvm_ext.so\", search_path=[\"../cpp/sym_add/outputs/libs\"])\n",
    "\n",
    "import tvm\n",
    "\n",
    "tvm._ffi._init_api(\"tvm_ext.testing\", __name__)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "下面便可以直接使用 `tvm_ext` 下的函数了："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<tvm.runtime.packed_func.PackedFunc at 0x7f992ebb3e80>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sym_add"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
